package com.imc.platform.business.lesson.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.imc.platform.base.category.pojo.Category;
import com.imc.platform.base.category.service.ICategoryService;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
* 类描述：课程管理
* 开发者：YT
* 创建日期：2018/9/18 18:49
*/
@Slf4j
@Controller
@RequestMapping("/lesson")
public class lessonController {

    @Autowired
    private ICategoryService categoryService;

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class ReqTuple{
        List<String> memberList;
        String roleId;
        Map<String,String>parmsMap;
        List<Map<String,Object>> parmsList;
    }

    /*
    * YT  2018/9/14 13:44
    * 方法描述：跳转到分类管理页面
    */
    @GetMapping("/showCategoryPage")
    public String categoryPage(){
        return "base/category/category";
    }

    /*
    * YT  2018/9/14 22:03
    * 方法描述：跳转分类操作页面
    */
    @RequestMapping(value = "/showOperationPage",method = RequestMethod.GET)
    public String showOperationPage(@RequestParam String categoryId, Model model){
        Map<String,Object>categoryMap = new HashMap<>();
        categoryMap.put("categoryId",categoryId);
        model.addAttribute("parms",categoryMap);
        return "base/category/categoryOperation";
    }

    /*
    * YT  2018/9/14 13:49
    * 方法描述：查询分类列表
    */
    @GetMapping("/queryCategoryList")
    @ResponseBody
    public ResultDTO queryRoleList(Pageable pageable){
        PageInfo<Category>queryCategoryList=categoryService.queryCategoryList(pageable.getPageNumber(),pageable.getPageSize(),null,null);
        JSONObject map = new JSONObject();
        map.put("total",queryCategoryList.getTotal());
        map.put("rows",queryCategoryList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /*
    * YT  2018/9/14 19:54
    * 方法描述：修改分类状态
    */
    @RequestMapping(value = "/updateState",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO updateStateByRoleId(@RequestParam String state,@RequestParam String roleId){
        return ResultDTO.builder().data(categoryService.updateState(state,roleId)).build();
    }

    /*
    * YT  2018/9/14 19:54
    * 方法描述：新增分类
    */
    @RequestMapping(value = "/addCategory",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO addCategory(@RequestBody ReqTuple reqTuple){
        return  ResultDTO.builder()
                .data(categoryService.delCategory("1"))
                .build();
    }

    /*
    * YT  2018/9/14 19:57
    * 方法描述：删除分类
    */
    @RequestMapping(value = "/delCategory",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO delCategory (@RequestParam String categoryId){
        return  ResultDTO.builder()
                .data(categoryService.delCategory(categoryId))
                .build();
    }

}
